package cn.edu.lingnan.dao;

import cn.edu.lingnan.pojo.Course;
import cn.edu.lingnan.util.DBUtil;

import java.awt.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CourseDaoMysqlImpl implements CourseDao {
    @Override
    public boolean addCourse(Course course) {
        boolean flag = false;
        Connection conn = null;
        PreparedStatement prep = null;

        try {
            conn = DBUtil.getInstance().getConnection();
            prep = conn.prepareStatement("INSERT INTO course (cid, cname, cflag) VALUES (?, ?, ?)");
            prep.setString(1, course.getCid());
            prep.setString(2, course.getCname());
            prep.setInt(3, course.getCflag());

            int result = prep.executeUpdate();
            if (result > 0) {
                flag = true;
            }

        } catch (SQLException e) {
            // It's a good practice to handle exceptions properly
            e.printStackTrace();
        } finally {
            // Ensure resources are closed in a finally block
            DBUtil.getInstance().close(prep, conn);
        }

        return flag;
    }

    @Override
    public int deleteCourseByCid(String _cid) {
        int flag=0;
        Connection conn=null;
        PreparedStatement prep=null;


        try {
            conn= DBUtil.getInstance().getConnection();
            prep=conn.prepareStatement("delete num from course where cid=?");
            prep.setString(1,_cid);
            prep.executeUpdate();
            flag=1;



        }  catch (SQLException e) {

        }finally {
            DBUtil.getInstance().close(prep,conn);
        }
        return flag;
    }

    @Override
    public Course findCourseByCid(String _cid) {
        Course course = new Course();
        Connection conn=null;
        Statement stat=null;
        ResultSet rs=null;

        try {
            conn=DBUtil.getInstance().getConnection();
            stat= conn.createStatement();
            rs= stat.executeQuery
                    ("select * from course where cid='"+_cid+"'");
            if (rs.next()){
                course.setCid(rs.getString("cid"));
                course.setCname(rs.getString("cname"));
                course.setCflag(rs.getInt("cflag"));
            }

        }  catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.getInstance().close(rs,stat,conn);
        }

        return course;
    }

    @Override
    public List<Course> findAllCourse() {
        List<Course> courses = new ArrayList<>();
        Connection conn = null;
        Statement stat = null;
        ResultSet rs = null;

        try {
            conn = DBUtil.getInstance().getConnection();
            stat = conn.createStatement();
            rs = stat.executeQuery("select * from course");

            while (rs.next()) {
                Course course = new Course();
                course.setCid(rs.getString("cid"));
                course.setCname(rs.getString("cname"));
                course.setCflag(rs.getInt("cflag"));
                courses.add(course);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.getInstance().close(rs, stat, conn);
        }

        return courses;
    }

}
